<?php
namespace app\admin\controller;
use app\service\orderService;
use think\Db;
use think\Log;
use think\db\Where;
class Reconciliation extends Common
{
    public function showlist()
    {
        $adminId = session('id');
        $usertype = Db::name('admin')->where('id',$adminId)->value('usertype');
        $this->assign('usertype',$usertype);
        //根据当前账号查询区域
        $com = (new Common())->pidTokenId();
        $whe=new Where();
        //$com['community_id']=implode(',',$com['community_id']);
        $whe['community_id'] = array('in',$com['community_id']);

        //根据账号查询停车场
        $parkinglist = (new orderService())->parkingList($whe);

        $this->assign('parkinglist',$parkinglist);
        $parkingNo = $this->getParkingNo();

        $parkingNo="800010,800011,1200018";
        //搜索
        $data = request()->get();
        //var_dump($data);
        //$where=new Where();
        $where=array();
        $clientList=array();
        if (!empty($data)){
            if(!empty($data['parkingNo'])){
                //$where['parkingNo'] = $data['parkingNo'];//停车场名称
                $where[]=['a.parkingNo','=',$data['parkingNo']];
                $clientList=Db::table("parking_client")->where("parkingNo",$data['parkingNo'])->select();
            }
            if(!empty($data['clientNo'])){

                $where[]=['a.clientNo','=',$data['clientNo']];
            }
            if(!empty($data['payType'])){

                $where[]=['a.payType','=',$data['payType']];
            }
            if(!empty($data['carPlateNo'])){

                $where[]=['a.carPlateNo','like','%'.$data['carPlateNo'].'%'];
            }

            if((!empty($data['start_time']))&&(empty($data['end_time']))){
                $where[]=['a.paytime','>=',date("YmdHis",strtotime($data['start_time']))];

            }
            if((!empty($data['end_time']))&&(empty($data['start_time']))){
                $where[]=['a.paytime','<=',date("YmdHis",strtotime($data['end_time']))];

            }
            if((!empty($data['start_time']))&&(!empty($data['end_time']))){

                $where[]=[ 'a.paytime',[">=",date("YmdHis",strtotime($data['start_time']))],["<=",date("YmdHis",strtotime($data['end_time']))],'and'];
            }
        }
        $this->assign("clientList",$clientList);
        //var_dump($where);
        //$where=array_filter($where);
        $export = $where;
        $export = json_encode($export);
        $export = str_replace("\"","|",$export);
        $this->assign('export',$export);
        $page = request()->has('page') ? request()->get('page') : 1;
        //多条件查询地库车位信息
        $payType="1,5,8";
        $query=array_filter($data);
        $orderlist =Db::name('car_order')
            ->alias("a")
            ->join("parking_client b","a.parkingNo=b.parkingNo")
            ->where("a.clientNo=b.clientNo")
            ->where('a.parkingNo','in',$parkingNo)
            ->where('a.payType','in',$payType)
            ->where($where)
            ->field("a.*,b.clientName")
            ->order("a.order_id desc")
            ->paginate(100, false, [
                'page'  =>  $page,
                'query' =>  $query
            ]);
        $money["ali"]=Db::name('car_order')->alias("a")->where('a.parkingNo','in',$parkingNo)->where($where)->where("a.payType","1")->sum("amountDue");
        $money["wx"]=Db::name('car_order')->alias("a")->where('a.parkingNo','in',$parkingNo)->where($where)->where("a.payType","2")->sum("amountDue");
        $money["cash"]=Db::name('car_order')->alias("a")->where('a.parkingNo','in',$parkingNo)->where($where)->where("a.payType","cash")->sum("amountDue");
        $money["coupon"]=Db::name('car_order')->alias("a")->where('a.parkingNo','in',$parkingNo)->where($where)->where("a.payType","3")->sum("amountDue");
        $this->assign("money",$money);
        $number = Db::name('car_order')->alias("a")->where('a.parkingNo','in',$parkingNo)->where($where)->count();
        $this->assign('number',$number);
        $this->assign('list',$orderlist);
        $this->assign('orderlist',$orderlist);
        foreach ($data as $k => $v) {
            $this->assign($k, $v);

        }
        return $this->fetch('showlist');
    }

    public function transferlist()
    {
        $data = request()->get();

        $where=array();
        if (!empty($data)){
            if(!empty($data['start_time'])){
                $where['day']=$data['start_time'];
                $start_time = $data['start_time'];
                $this->assign('start_time',$start_time);
            }
            $this->assign('where',$where);
            $where = array_filter($where);
        }else{

        }

        $export = $where;
        $export = json_encode($export);
        $export = str_replace("\"","|",$export);
        $this->assign('export',$export);
        $page = request()->has('page') ? request()->get('page') : 1;
        //多条件查询地库车位信息
        $orderlist =Db::name('car_order_reconciliation_statis')->where($where)->order('id desc')->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);

        $number =Db::name('car_order_reconciliation_statis')->where($where)->count();
        $unpay_money =Db::name('car_order_reconciliation_statis')->where($where)->where("state","1")->sum("due_amount");

        $pay_money =Db::name('car_order_reconciliation_statis')->where($where)->where("state","2")->sum("due_amount");
        $this->assign("unpay_money",$unpay_money);
        $this->assign("pay_money",$pay_money);
        $this->assign('number',$number);
        $this->assign('list',$orderlist);
        $this->assign('orderlist',$orderlist);
        foreach ($data as $k => $v) {
            if(is_array($v)){

                $v = str_replace('%','',$v[1]);
                $this->assign($k, $v);


            }else{
                $this->assign($k, $v);
            }
        }
        return $this->fetch();
    }


    public function transfer(){
        $id=request()->get("id");
        $info=Db::table("car_order_reconciliation_statis")->where("id",$id)->find();
        if(empty($info)){
            return ;
        }else{
            if($info["state"]=="1"){
                $res=Db::table("car_order_reconciliation_statis")->where("id",$id)->update(["state"=>"2","update_time"=>date('Y-m-d H:i:s')]);
                $this->success("转账成功");
            }else{
                return ;
            }
        }

    }

    public function edit(){
        if(request()->isPost()){
            $data=request()->post();
            $res=Db::table("car_order_reconciliation_statis")->update($data);
            if($res){
                return "修改成功";
            }else{
                return "修改失败";
            }
        }else{
            $id=request()->get("id");
            $info=Db::table("car_order_reconciliation_statis")->where("id",$id)->find();
            if(empty($info)){
                return "数据不存在";
            }
            $this->assign("info",$info);
            return $this->fetch();
        }

    }
}